/**********************************************************************************
 * 
 * CookieManager.java
 * This class manages Cookies. The main use for cookies in our project is keeping
 * the chosen language for the user, to enable internationalization between pages
 * 
 **********************************************************************************/

package ict.kth.se.manager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import ict.kth.se.web.util.CookieUtils;

public class CookieManager {
	
	private static final String LANG_COOKIE_NAME = "language";
	
	public CookieManager(){}
	
	/**
	 * handleLanguageCookie(), handles the language cookie
	 * @param modelAndView the model and viw
	 * @param request The HTTPServletRequest
	 * @param response The HTTPServletResponse
	 * @param paramLanguage Language
	 * @return
	 */
	public static ModelAndView handleLanguageCookie(ModelAndView modelAndView, HttpServletRequest request, 
			HttpServletResponse response, String paramLanguage){
		//If language param is existing than it is that language, update cookie
		if(paramLanguage != null){
			modelAndView.addObject("language", paramLanguage);
			CookieUtils.addCookie(LANG_COOKIE_NAME, paramLanguage, response);
		}
		//If language param doesn't exist but there is language cookie use it
		else{
			String cookieValue = CookieUtils.getCookieValue(request, LANG_COOKIE_NAME);
			if(cookieValue != null){
				modelAndView.addObject("language", cookieValue);
			}
		}
		
		return modelAndView;
	}
}
